
package com.skyc.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.sql.rowset.CachedRowSet;

import com.skyc.dao.ClothesDao;
import com.skyc.dao.ShopDao;
import com.skyc.dao.UserDao;
import com.skyc.entity.Shop;
import com.skyc.util.JDBCUtil;

/**
 * 购物车
 * 
 * @author BWJ
 * @date 2021年5月15日 下午2:33:19
 */

public class ShopDaoImpl implements ShopDao {
	JDBCUtil ju = JDBCUtil.getJDBCUtil();
	ClothesDao cd = new ClothesDaoImpl();
	UserDao ud = new UserDaoImpl();

	@Override
	public List<Shop> queryByUID(String uid) {
		String sql = "SELECT * FROM t_shop WHERE u_id=?";
		CachedRowSet crs = ju.query(sql, uid);
		return returnShops(crs);
	}

	@Override
	public boolean deleteBySID(String sid) {

		return ju.update("DELETE FROM t_shop WHERE s_id=?", sid);
	}

	// 工具
	private Shop returnShopUtil(CachedRowSet crs) throws SQLException {
		Shop s = new Shop();
		s.setSid(crs.getInt("s_id"));
		s.setSnum(crs.getInt("s_num"));
		s.setUser(ud.queryById(crs.getInt("u_id") + ""));
		s.setClothes(cd.queryByID(crs.getInt("c_id") + ""));
		return s;
	}

	// 工具
	private Shop returnShop(CachedRowSet crs) {
		Shop s = null;
		try {
			if (crs.next()) {
				s = returnShopUtil(crs);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			ju.closeCacheRowSet(crs);
		}
		return s;

	}

	// 工具
	private List<Shop> returnShops(CachedRowSet crs) {
		List<Shop> cs = new ArrayList<Shop>();
		try {
			while (crs.next()) {
				Shop s = null;
				s = returnShopUtil(crs);
				cs.add(s);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			ju.closeCacheRowSet(crs);
		}
		return cs;

	}

	@Override
	public boolean updateBySID(String sid, String snum) {
		String sql = "UPDATE t_shop SET s_num=? WHERE s_id=?";
		return ju.update(sql, snum, sid);
	}

	@Override
	public int returnCountByUID(String uid) {
		String sql = "SELECT COUNT(u_id) count FROM t_shop WHERE u_id=?";
		CachedRowSet crs = ju.query(sql, uid);
		int count = 0;
		try {
			if (crs.next()) {
				count = crs.getInt("count");
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			ju.closeCacheRowSet(crs);
		}
		return count;
	}

	@Override
	public boolean addShop(String uid, String cid,String snum) {
		String sql = "INSERT INTO t_shop (s_num,u_id,c_id) values(?,?,?)";
		return ju.update(sql, snum,uid, cid);
	}

	@Override
	public boolean deleteAll(String uid) {
		return ju.update("DELETE FROM t_shop WHERE u_id=?", uid);
	}

	@Override
	public Shop queryBySID(String sid) {
		String sql="SELECT * FROM t_shop WHERE s_id=?";
		CachedRowSet crs=ju.query(sql, sid);
		return returnShop(crs);
	}

	@Override
	public Shop queryByCIDUID(String cid,String uid) {
		String sql="SELECT * FROM t_shop WHERE c_id=? AND u_id=?";
		CachedRowSet crs=ju.query(sql, cid,uid);
		return returnShop(crs);
		
	}

}
